import { EventTouch, Node } from 'cc';
import { ccclass, ccmodifier } from '../decorators';
import { BaseView } from './BaseView';

/**
 * - 作者: DoooReyn
 * - 日期: 2023.10.31
 * - 名称: Top
 * - 描述: 视图-顶层
 * - 说明: 提供触摸穿透功能，提供点击特效
 */
@ccclass()
@ccmodifier('Top')
export class Top extends BaseView {
    /** 事件类型 */
    public static readonly EventType = {
        /** 触摸屏幕 */
        TouchSceen: 'touch-screen',
    };

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchScreen, this, false);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchScreen, this, false);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchScreen, this, false);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchScreen, this, false);
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this.onTouchScreen, this, false);
        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchScreen, this, false);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchScreen, this, false);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchScreen, this, false);
    }

    /**
     * 触摸屏幕时调用
     * @param e 触摸事件
     */
    protected onTouchScreen(e: EventTouch): void {
        e.preventSwallow = true;
        if (e.type === Node.EventType.TOUCH_START) {
            const { x, y } = e.getUILocation();
            this.logger.debug(`触摸屏幕: ${x | 0},${y | 0}`);
            this.node.emit(Top.EventType.TouchSceen, e);
        }
    }

    public async exit(): Promise<void> {
        this.logger.debug('Top 在舞台退出时不需要处理哦！');
    }
}
